package com.wxy.jk.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.promeg.pinyinhelper.Pinyin;
import com.wxy.jk.common.enchance.MpLambdaQueryWrapper;
import com.wxy.jk.common.enchance.MpPage;
import com.wxy.jk.common.mapStruct.MapStructs;
import com.wxy.jk.common.util.Streams;
import com.wxy.jk.mapper.PlateRegionMapper;
import com.wxy.jk.pojo.po.PlateRegion;
import com.wxy.jk.pojo.vo.PageVo;
import com.wxy.jk.pojo.vo.list.PlateRegionVo;
import com.wxy.jk.pojo.vo.list.ProvinceVo;
import com.wxy.jk.pojo.vo.req.page.CityPageReqVo;
import com.wxy.jk.pojo.vo.req.page.ProvincePageReqVo;
import com.wxy.jk.service.PlateRegionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
@Transactional
public class PlateRegionServiceImpl extends ServiceImpl<PlateRegionMapper, PlateRegion> implements PlateRegionService {


    @Override
    public boolean save(PlateRegion entity){
        processPinyin(entity);
        return super.save(entity);
    }
    @Override
    public boolean updateById(PlateRegion entity){
        processPinyin(entity);
        return super.updateById(entity);
    }

private void processPinyin(PlateRegion region){
    String name = region.getName();
    if(StringUtils.isEmpty(name))return;
    region.setPinyin(Pinyin.toPinyin(name, "_"));
}


    @Override
    @Transactional(readOnly = true)
    public PageVo<PlateRegionVo> listProvinces(ProvincePageReqVo query) {
        MpLambdaQueryWrapper<PlateRegion> wrapper = new MpLambdaQueryWrapper<>();
       wrapper.like(query.getKeyword(), PlateRegion::getName, PlateRegion::getPlate, PlateRegion::getPinyin);
       wrapper.eq(PlateRegion::getParentId, 0);
        return baseMapper.selectPage(new MpPage<>(query), wrapper).buildVo(MapStructs.INSTANCE::po2vo);
    }

    @Override
    @Transactional(readOnly = true)
    public PageVo<PlateRegionVo> listCities(CityPageReqVo query) {
        MpLambdaQueryWrapper<PlateRegion> wrapper = new MpLambdaQueryWrapper<>();
        wrapper.like(query.getKeyword(), PlateRegion::getName, PlateRegion::getPlate, PlateRegion::getPinyin);
        Integer parentId = query.getParentId();
        if(parentId != null && parentId > 0) {
            wrapper.eq(PlateRegion::getParentId, parentId);
        }else {
            wrapper.ne(PlateRegion::getParentId, 0);
        }
        return baseMapper.selectPage(new MpPage<>(query), wrapper).buildVo(MapStructs.INSTANCE::po2vo);

    }

    @Override
    @Transactional(readOnly = true)
    public List<PlateRegionVo> listProvinces() {
        MpLambdaQueryWrapper<PlateRegion> wrapper = new MpLambdaQueryWrapper<>();
        wrapper.eq(PlateRegion::getParentId, 0);
        wrapper.orderByAsc(PlateRegion::getPinyin);
        return Streams.map(baseMapper.selectList(wrapper), MapStructs.INSTANCE::po2vo);
    }

    @Override
        public List<ProvinceVo> listRegions() {

            return baseMapper.selectRegions();
    }


}
